package com.gitee.mocoffee.misc;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;


/**
 * 日期时间工具类
 * @author lijia
 */
public class DatetimeKit {
    public static final DatetimeKit me = new DatetimeKit();

    private DatetimeKit() {}

    public Date localDate2Date(LocalDate source) {
        return source == null ? null : Date.from(source.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public Date localDateTime2Date(LocalDateTime source) {
        return source == null ? null : Date.from(source.atZone(ZoneId.systemDefault()).toInstant());
    }

    public LocalDateTime date2LocalDateTime(Date date) {
        return date == null ? null : LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public LocalDateTime long2LocalDateTime(Long l) {
        return l == null ? null : date2LocalDateTime(new Date(l));
    }

    public Date clearTimes(Date source) {
        if (source == null) return null;
        LocalDateTime ldt = LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
        return localDate2Date(ldt.toLocalDate());
    }

    public Date addDays(Date base, int days) {
        return new Date(base.getTime() + days * 86400 * 1000);
    }
}
